package com.copa.module.user.controller;

import com.copa.domain.ResponseDto;
import com.copa.module.user.domain.dto.UserAddressDto;
import com.copa.module.user.domain.entity.UserAddressEntity;
import com.copa.module.user.domain.entity.UserEntity;
import com.copa.module.user.domain.vo.UserAddressAddVo;
import com.copa.module.user.domain.vo.UserAddressUpdateVo;
import com.copa.module.user.service.UserAddressService;
import com.copa.utils.WechatUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * @author copa
 * @createDate 2021-01-14 12:28
 * @function
 */
@RestController
@Api(tags = {"我的地址接口"})
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("/address/list")
    @ApiOperation(value = "我的地址列表")
    public ResponseDto<List<UserAddressDto>> list(@AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return userAddressService.list(openId);
    }

    @PostMapping("/address/add")
    @ApiOperation(value = "添加地址")
    public ResponseDto<String> add(@RequestBody UserAddressAddVo userAddressAddVo,
                                   @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return userAddressService.add(userAddressAddVo, openId);
    }

    @GetMapping("/address/get")
    @ApiOperation(value = "查找特定地址")
    public ResponseDto<UserAddressDto> get(@ApiParam("地址id") @RequestParam("addressId") Long addressId,
                                   @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return userAddressService.get(addressId, openId);
    }

    @PostMapping("/address/put")
    @ApiOperation(value = "修改地址")
    public ResponseDto<String> put(@RequestBody UserAddressUpdateVo userAddressUpdateVo,
                                   @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return userAddressService.put(userAddressUpdateVo, openId);
    }

    @GetMapping("/address/getDefaultAddress")
    @ApiOperation(value = "查找默认地址")
    public ResponseDto<UserAddressDto> getDefaultAddress(@AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return userAddressService.getDefaultAddress(openId);
    }
}
